package lego.Moving;

import java.util.Random;

import lego.animal.*;
import lego.jbv.Display;
import lejos.nxt.LCD;
import lejos.nxt.SensorPort;
import lejos.nxt.TouchSensor;
import lejos.util.Delay;

public class Avoid implements Behavior {
	private TouchSensor touchLeft;
	private TouchSensor touchRight;
	private boolean suppressed = false;
	private final int turnDelay = 1000;
	private Random random = new Random();

	public Avoid() {
		touchLeft = new TouchSensor(SensorPort.S1);
		touchRight = new TouchSensor(SensorPort.S4);
	}

	@Override
	public int takeControl() {
		if (touchLeft.isPressed() || touchRight.isPressed())
			return 100;

		return 0;
	}

	@Override
	public void action() {
		Display.setStatus("AVOID");
		suppressed = false;
		
		Car.drive(-Display.getNormalSpeed(), -Display.getNormalSpeed());
		Delay.msDelay(200);
		
		if (touchLeft.isPressed() && !touchRight.isPressed()) {
			Car.rotate(true);
		} else if (touchRight.isPressed() && !touchLeft.isPressed()) {
			Car.rotate(false);
		} else {
			if (random.nextBoolean()) {
				Car.rotate(true);
				Car.rotate(true);
			}
			else {
				Car.rotate(false);
				Car.rotate(false);
			}
		}
	}

	@Override
	public void suppress() {
		suppressed = true;
	}

}
